home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Application Source ƒ / 68k Internet Config ƒ / C Source ƒ / IC IconSuites.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-02  |  4.1 KB  |  202 lines  |  [TEXT/SPM ]

  1. /*
  2.     IC IconSuites.c
  3.     
  4.     
  5. */
  6.  
  7. #include <Limits.h>
  8. #include <Icons.h>
  9.  
  10. #include "IC Globals.h"
  11. #include "IC Misc Subs.h"
  12.  
  13. #include "IC IconSuites.h"
  14.  
  15. CacheRecord cache[cache_max+1];
  16. long usage;
  17. Handle default_application_suite;
  18. Handle default_document_suite;
  19.  
  20. Byte icon_buffer[1024];
  21.  
  22. Boolean GetTheIcon(DTPBRec* pbdt,SignedByte icon_to_get,OSType fcreator,OSType ftype,short dtrn){
  23.     pbdt->ioDTRefNum=dtrn;
  24.     pbdt->ioTagInfo=0;
  25.     pbdt->ioIconType=icon_to_get;
  26.     pbdt->ioFileCreator=fcreator;
  27.     pbdt->ioFileType=ftype;
  28.     return PBDTGetIconSync(pbdt)==noErr;
  29. }
  30.  
  31. OSErr GetDTDBIcon(OSType ftype,OSType fcreator,short* cookie,SignedByte icon_to_get,DTPBRec* pbdt){
  32.     OSErr oe;
  33.     short i;
  34.     Boolean found=false;
  35.     Str63 junk;
  36.     short vrefnum;
  37.     long crdate;
  38.     
  39.     if (System7){
  40.         if (*cookie==0){
  41.             i=1;
  42.             oe=noErr;
  43.             do {
  44.                 vrefnum=0;
  45.                 junk[0]=0;
  46.                 oe=ICGetVolInfo(junk,&vrefnum,i,&crdate);
  47.                 i++;
  48.                 if (oe==noErr){
  49.                     pbdt->ioNamePtr=(StringPtr)0;
  50.                     pbdt->ioVRefNum=vrefnum;
  51.                     oe=PBDTGetPath(pbdt);
  52.                     
  53.                     if (oe==noErr){
  54.                         if (GetTheIcon(pbdt,icon_to_get,fcreator,ftype,pbdt->ioDTRefNum)){
  55.                             *cookie=pbdt->ioDTRefNum;
  56.                             found=true;
  57.                         }
  58.                     }
  59.                     oe=noErr;
  60.                 }
  61.             } while ((!found)&&(oe==noErr));
  62.         } else
  63.             found=GetTheIcon(pbdt,icon_to_get,fcreator,ftype,*cookie);
  64.     }
  65.     if (found)
  66.         oe=noErr;
  67.     else
  68.         oe=afpItemNotFound;
  69.     
  70.     return oe;
  71. }
  72.  
  73. OSErr GetDTDBIconH(OSType ftype,OSType fcreator,short* cookie,SignedByte icon_to_get,Handle* iconh){
  74.     DTPBRec dtpb;
  75.     OSErr err;
  76.     
  77.     *iconh=(Handle)0;
  78.     dtpb.ioDTBuffer=(char*)icon_buffer;
  79.     dtpb.ioDTReqCount=sizeof(Byte)*1024;
  80.     
  81.     err=GetDTDBIcon(ftype,fcreator,cookie,icon_to_get,&dtpb);
  82.     if (err==noErr)
  83.         err=PtrToHand((Ptr)icon_buffer,iconh,dtpb.ioDTActCount);
  84.     
  85.     if (err!=noErr){
  86.         DisposeHandle(*iconh);
  87.         *iconh=(Handle)0;
  88.     } else
  89.         MoveHHi(*iconh);
  90.     
  91.     return err;
  92. }
  93.  
  94. OSErr GetDTDBAddSuite(Handle suite,OSType ftype,OSType fcreator,short* cookie,SignedByte icon_to_get,OSType icon_to_put){
  95.     OSErr err;
  96.     Handle iconh;
  97.     
  98.     err=GetDTDBIconH(ftype,fcreator,cookie,icon_to_get,&iconh);
  99.     
  100.     if (err==noErr)
  101.         err=AddIconToSuite(iconh,suite,icon_to_put);
  102.     
  103.     if (err!=noErr){
  104.         DisposeHandle(iconh);
  105.     }
  106.     
  107.     return err;
  108. }
  109.  
  110. OSErr GetDTDBIconSuiteUncached(OSType ftype,OSType fcreator,Handle* suite){
  111.     OSErr err;
  112.     short cookie;
  113.     
  114.     *suite=(Handle)0;
  115.     err=NewIconSuite(suite);
  116.     if (err==noErr){
  117.         cookie=0;
  118.         if (GetDTDBAddSuite(*suite,ftype,fcreator,&cookie,kLargeIcon,large1BitMask)==noErr){
  119.             GetDTDBAddSuite(*suite,ftype,fcreator,&cookie,kLarge4BitIcon,large4BitData);
  120.             GetDTDBAddSuite(*suite,ftype,fcreator,&cookie,kLarge8BitIcon,large8BitData);
  121.         } else 
  122.             err=afpItemNotFound;
  123.     }
  124.     
  125.     if (err!=noErr){
  126.         if (*suite!=(Handle)0){
  127.             DisposeIconSuite(*suite,true);
  128.             *suite=(Handle)0;
  129.         }
  130.     } else
  131.         MoveHHi(*suite);
  132.     
  133.     return err;
  134. }
  135.  
  136. OSErr GetDTDBIconSuiteCached(OSType ftype,OSType fcreator,Handle* suite){
  137.     OSErr err=(OSErr)-1;
  138.     short i,j;
  139.     long m;
  140.     
  141.     *suite=(Handle)0;
  142.     
  143.     for (i=0;i<cache_max;i++){
  144.         if ((cache[i].usage>0)&&((cache[i].ftype==ftype)&&(cache[i].fcreator==fcreator))){
  145.             *suite=cache[i].suite;
  146.             err=noErr;
  147.             cache[i].usage++;
  148.             usage++;
  149.             return err;
  150.         }
  151.     }
  152.     
  153.     if (err!=noErr){
  154.         m=LONG_MAX;
  155.         for (i=0;i<cache_max;i++){
  156.             if (cache[i].usage<m){
  157.                 j=i;
  158.                 m=cache[i].usage;
  159.             }
  160.         }
  161.         err=GetDTDBIconSuiteUncached(ftype,fcreator,suite);
  162.         if (err==noErr){
  163.             if (m>0)
  164.                 DisposeIconSuite(cache[j].suite,true);
  165.             cache[j].suite=*suite;
  166.             cache[j].ftype=ftype;
  167.             cache[j].fcreator=fcreator;
  168.             cache[j].usage=usage;
  169.             usage++;
  170.         }
  171.     }
  172.     
  173.     if ((err==noErr)&&(*suite==(Handle)0))
  174.         err=resNotFound;
  175.     
  176.     if (err!=noErr){
  177.         *suite=default_document_suite;
  178.         if ((ftype=='APPL')&&(default_application_suite!=(Handle)0))
  179.             *suite=default_application_suite;
  180.     }
  181.     
  182.     return err;
  183. }
  184.  
  185. void InitICIconSuites(void){
  186.     short i;
  187.     
  188.     for (i=0;i<cache_max;i++)
  189.         cache[i].usage=-1;
  190.     
  191.     default_application_suite=(Handle)0;
  192.     default_document_suite=(Handle)0;
  193.     
  194.     if (System7){
  195.         if (GetIconSuite(&default_document_suite,kDocSuiteID,svAllLargeData)!=noErr)
  196.             default_document_suite=(Handle)0;
  197.         if (GetIconSuite(&default_application_suite,kAppSuiteID,svAllLargeData)!=noErr)
  198.             default_application_suite=(Handle)0;
  199.     }
  200. }
  201.  
  202.